package app

import (
	"strconv"

	"gitee.com/cyanbleeze/go-image-management/global"
	"github.com/gin-gonic/gin"
)

func GetPage(c *gin.Context) int {
	page, err := strconv.Atoi(c.Query("page"))
	if err != nil {
		return 1
	}
	if page <= 0 {
		return 1
	}
	return page
}

func GetPageSize(c *gin.Context) int {
	pageSize, err := strconv.Atoi(c.Query("page_size"))
	if err != nil || pageSize <= 0 {
		return global.AppSetting.DefaultPageSize
	}
	if pageSize > global.AppSetting.DefaultPageSize {
		return global.AppSetting.DefaultPageSize
	}
	return pageSize
}

func GetPageOffset(page int, pageSize int) int {
	return (page - 1) * pageSize
}
